﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Collections.Specialized;
using System.Diagnostics;
using CServiceStack.ServiceHost;
using System.Net;

namespace SomeIISServiceHost
{
    public class OutgoingHubAttribute : Attribute, IHasResponseFilter
    {


        public IHasResponseFilter Copy()
        {
            return this;
        }

        public int Priority
        {
            get
            {
                return -1;
            }
        }





        public void ResponseFilter(IHttpRequest req, IHttpResponse res, object responseDto)
        {
            //res.OutputStream = new System.IO.Compression.GZipStream(res.OutputStream, System.IO.Compression.CompressionMode.Compress, true);
        }
    }
    public static class Watcher
    {
        public static string StartTime { get; set; }
    }
    public class Global : System.Web.HttpApplication
    {
        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            this.Response.Filter = new System.IO.Compression.GZipStream(this.Response.Filter, System.IO.Compression.CompressionMode.Compress, true);
            Response.AppendHeader("Content-Encoding", "gzip");
        }

        void Application_Start(object sender, EventArgs e)
        {
            ServicePointManager.DefaultConnectionLimit = int.MaxValue;
            // Code that runs on application startup
            MockServiceHost host = new MockServiceHost();
            Watcher.StartTime = DateTime.Now.ToLongTimeString();
            //host.RequestFilters.Add((req, res, requestDto) =>
            //{
            //    NameValueCollection headers = req.Headers;

                
            //    foreach (string h in headers.Keys)
            //    {
            //        Trace.WriteLine(string.Format("SOA: header:{0} value{1}", h, headers[h].ToString()));
            //        Trace.Flush();
            //    }

            //    Trace.WriteLine(string.Format("SOA: body{0}", req.GetRawBody()));
            //    Trace.Flush();
            //});

            //host.ResponseFilters.Add((req, res, requestDto) =>
            //    {
            //    }
            //    );
            host.Init();

        }

        void Application_End(object sender, EventArgs e)
        {
            //  Code that runs on application shutdown

        }

        void Application_Error(object sender, EventArgs e)
        {
            // Code that runs when an unhandled error occurs
            Trace.TraceError(e.ToString());
        }

        void Session_Start(object sender, EventArgs e)
        {
            // Code that runs when a new session is started

        }

        void Session_End(object sender, EventArgs e)
        {
            // Code that runs when a session ends. 
            // Note: The Session_End event is raised only when the sessionstate mode
            // is set to InProc in the Web.config file. If session mode is set to StateServer 
            // or SQLServer, the event is not raised.

        }

    }
}
